package com.sihan.framework.etm.common.util;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * RTK 通过电压判电池电量
 */
@Slf4j
public class QuantityUtils {


    //电池低电量预警阀值
    public static final BigDecimal electricityPercent = new BigDecimal("20.00");
    //中等电量阀值
    public static final BigDecimal MEDIUM = new BigDecimal("75.00");
    //最大待机天数
    public static final Integer voltageWaitDays = 940;
    //最多上传次数
    public static final Integer voltageCount = 420;


    /**
     * 格式化电量
     * RTK正常电压 4.6-3.3
     *
     * @return
     */
    public static BigDecimal formattedQuantity(BigDecimal voltage) {
        BigDecimal maxVoltage = new BigDecimal("4.20");
        BigDecimal minVoltage = new BigDecimal("3.40");
        if (null != voltage && voltage.compareTo(BigDecimal.ZERO) != 0) {
            //如果电压大于或等于maxVoltage 设备电量100%
            if (voltage.compareTo(maxVoltage) > -1) {
                return new BigDecimal("100.0");
            }
            //如果电压小于于或等于minVoltage设备电量0%
            if (minVoltage.compareTo(voltage) > -1) {
                return BigDecimal.ZERO;
            }
            //如果电压在maxVoltage-minVoltage区间 则取百分比
            BigDecimal subtract = voltage.subtract(minVoltage).setScale(3, RoundingMode.UP);
            return (subtract.divide(new BigDecimal("8.00"), RoundingMode.HALF_UP)).multiply(new BigDecimal("1000.0"));
        }
        return BigDecimal.ZERO;
    }

    /**
     * 根据电压判断电池电量是否处于低电量
     */
    public static Boolean powerAlarm(BigDecimal voltage) {
        return formattedQuantity(voltage).compareTo(electricityPercent) <= 0;
    }

    /**
     * 根据电量判断电池电量是否处于低电量
     *
     * @param electricQuantity 电量
     */
    public static Boolean electricitypowerAlarm(BigDecimal electricQuantity) {
        return electricQuantity.compareTo(electricityPercent) <= 0;
    }

    /**
     * 根据电压判断电池电量
     *
     * @param voltage
     * @return
     */
    public static String powerToJudge(BigDecimal voltage) {
        String str = "低";
        if (formattedQuantity(voltage).compareTo(electricityPercent) <= 0) {
            return "低";
        }
        if (formattedQuantity(voltage).compareTo(electricityPercent) > 0 && formattedQuantity(voltage).compareTo(MEDIUM) < 0) {
            return "中";
        }
        if (formattedQuantity(voltage).compareTo(MEDIUM) >= 0) {
            return "高";
        }
        return str;
    }

    /**
     * 通过发送次数与待机天数计算电量
     *
     * @param sendCount 发送的次数
     * @param waitDays  待机天数
     * @return
     * @return BigDecimal
     * @description //剩余电量 //S =（M-x-y*A)/M
     * @auther 韩兵
     * @Date 2021年6月4日
     */
    public static BigDecimal calculateVoltage(Integer sendCount, Integer waitDays) {
        BigDecimal a = new BigDecimal(voltageCount).divide(new BigDecimal(voltageWaitDays), 3, RoundingMode.HALF_UP);
        BigDecimal x = new BigDecimal(sendCount).divide(new BigDecimal(4), 3, RoundingMode.HALF_UP);
        return (new BigDecimal(voltageCount).subtract(x).subtract(
                new BigDecimal(waitDays).multiply(a))).divide(new BigDecimal(voltageCount), 3, RoundingMode.HALF_UP);
    }

    /**
     * 根据电量判断电池电量
     */
    public static String electricQuantityToJudge(BigDecimal electricQuantity) {
        String str = "低";
        if (electricQuantity.compareTo(electricityPercent) <= 0) {
            return "低";
        }
        if (electricQuantity.compareTo(electricityPercent) > 0 && electricQuantity.compareTo(MEDIUM) < 0) {
            return "中";
        }
        if (electricQuantity.compareTo(MEDIUM) >= 0) {
            return "高";
        }
        return str;
    }

    public static BigDecimal formattedNewQuantity(BigDecimal voltage,BigDecimal minVoltage,BigDecimal maxVoltage) {
        if (null != voltage && voltage.compareTo(BigDecimal.ZERO) != 0) {
            //如果电压大于或等于maxVoltage 设备电量100%
            if (voltage.compareTo(maxVoltage) > -1) {
                return new BigDecimal("100.0");
            }
            //如果电压小于于或等于minVoltage设备电量0%
            if (minVoltage.compareTo(voltage) > -1) {
                return BigDecimal.ZERO;
            }
            //如果电压在maxVoltage-minVoltage区间 则取百分比
            BigDecimal subtract = voltage.subtract(minVoltage).setScale(3, RoundingMode.UP);
            return (subtract.divide(maxVoltage.subtract(minVoltage), RoundingMode.HALF_UP)).multiply(new BigDecimal("100.0"));
        }
        return BigDecimal.ZERO;
    }

    public static void main(String[] args){
        System.out.println(formattedNewQuantity(new BigDecimal(7.75),new BigDecimal(6),new BigDecimal(8.4)));
    }


}
